package com.fxm.service.impl;

import com.fxm.dao.RecordMapper;
import com.fxm.domain.Record;
import com.fxm.enums.AnswerEnum;
import com.fxm.returns.RespCode;
import com.fxm.returns.RespEntity;
import com.fxm.service.RecordService;
import com.fxm.vo.param.RecordParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 * 答题记录 服务实现类
 * </p>
 *
 * @author fxm
 * @since 2021-01-07
 */
@Slf4j
@Service
public class RecordServiceImpl implements RecordService {

    @Autowired
    private RecordMapper recordMapper;

    @Override
    public RespEntity insertRecordAnswer(RecordParam recordParam) {
        if (Objects.isNull(recordParam)) {
            log.info("参数为空");
            return new RespEntity(RespCode.PARAMETERS_ERROR);
        }
        Record record = new Record();
        record.setOpenid(recordParam.getOpenid());
        record.setQuestionId(recordParam.getQuestionId());
        AnswerEnum answerEnum = AnswerEnum.getInstance(recordParam.getIsRight());
        if (Objects.nonNull(answerEnum)) {
            record.setIsRight(answerEnum.getDesc());
        }
        recordMapper.insertRecord(record);
        log.info("同步用户答题记录成功，record={}", record);
        return new RespEntity(RespCode.SUCCESS);
    }
}
